package com.xuerhua.yxt.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xuerhua.yxt.common.Const;
import com.xuerhua.yxt.common.MenuConst;
import com.xuerhua.yxt.common.enums.DictDataCode;
import com.xuerhua.yxt.common.enums.RelationType;
import com.xuerhua.yxt.dao.AttachmentListMapper;
import com.xuerhua.yxt.dao.OnlineRepairMapper;
import com.xuerhua.yxt.dao.RepairBuildingMapper;
import com.xuerhua.yxt.dao.RepairTypeMapper;
import com.xuerhua.yxt.dao.SchoolWechatTemplateMapper;
import com.xuerhua.yxt.model.AttachmentList;
import com.xuerhua.yxt.model.OnlineRepair;
import com.xuerhua.yxt.model.RepairBuilding;
import com.xuerhua.yxt.model.RepairType;
import com.xuerhua.yxt.model.SchoolInfo;
import com.xuerhua.yxt.model.TeacherInfo;
import com.xuerhua.yxt.page.Page;
import com.xuerhua.yxt.reqVo.AttachRelationReqVO;
import com.xuerhua.yxt.reqVo.FailRepairReqVO;
import com.xuerhua.yxt.service.IOnlineRepairService;
import com.xuerhua.yxt.service.ISchoolInfoService;
import com.xuerhua.yxt.service.ITeacherService;
import com.xuerhua.yxt.utils.BeanUtils;
import com.xuerhua.yxt.view.OnlineRepairView;
import com.xuerhua.yxt.wechat.utils.TemplateUtil;

/**
 * 故障报修的实现类
 * 
 * */
@Service("onlineRepairService")
public class OnlineRepairServicelmpl implements IOnlineRepairService {

	@Autowired
	private OnlineRepairMapper onlineRepairMapper;
	@Autowired
	private RepairBuildingMapper repairBuildingMapper;
	@Autowired
	private RepairTypeMapper repairTypeMapper;
	@Autowired
	private AttachmentListMapper attachmentListMapper;
	@Autowired
	private ITeacherService teacherService;
	@Autowired
	private ISchoolInfoService schoolInfoService;
	@Autowired
	private SchoolWechatTemplateMapper schoolWechatTemplateMapper;

	@Override
	public int insertSelective(OnlineRepair record) {
		return onlineRepairMapper.insertSelective(record);
	}

	@Override
	public OnlineRepair selectByPrimaryKey(Integer id) {
		return onlineRepairMapper.selectByPrimaryKey(id);
	}

	@Override
	public int updateByPrimaryKeySelective(OnlineRepair record) {
		return onlineRepairMapper.updateByPrimaryKeySelective(record);
	}

	@Override
	public List<OnlineRepair> selectListByPage(Page<OnlineRepair> page) {
		return onlineRepairMapper.selectListByPage(page);
	}

	@Override
	public List<RepairType> selectRepairTypeList(Integer schoolId) {
		return repairTypeMapper.selectList(schoolId);
	}

	@Override
	public int addAndUpdateRepairType(RepairType record) {
		if(record.getId() != null) {
			return repairTypeMapper.updateByPrimaryKeySelective(record);
		}
		return repairTypeMapper.insertSelective(record);
	}
	
	
	@Override
	public int delRepairType(List<String> ids) {
		return repairTypeMapper.delByPrimaryKey(ids);
	}

	@Override
	public int delRepairBuilding(List<String> ids) {
		return repairBuildingMapper.delByPrimaryKey(ids);
	}

	@Override
	public int addAndUpdateRepairBuilding(RepairBuilding record) {
		if(record.getId() != null) {
			return repairBuildingMapper.updateByPrimaryKeySelective(record);
		}
		return repairBuildingMapper.insertSelective(record);
	}

	@Override
	public List<RepairBuilding> selectRepairBuildingList(Integer schoolId) {
		return repairBuildingMapper.selectList(schoolId);
	}

	@Override
	public int isRepairMan(Integer teacherId) {
		return onlineRepairMapper.selectCountForRepair(teacherId);
	}

	@Override
	public int addRepaire(FailRepairReqVO reqVO) {
		if(reqVO.getAttachIds() != null) {
			reqVO.setIsAttach(1);
		}
		onlineRepairMapper.insertSelective(reqVO);
		if(reqVO.getAttachIds() != null) {
			AttachRelationReqVO attachReqVO = new AttachRelationReqVO();
			attachReqVO.setRelationId(Long.valueOf(reqVO.getId()));
			attachReqVO.setAttachIds(reqVO.getAttachIds().split(","));
			attachReqVO.setSchoolId(reqVO.getSchoolId());
			return attachmentListMapper.updateRelationIdByIds(attachReqVO);
		}
		new Thread(new OnlineRepairExecutor(reqVO, 1)).start();
		return 1;
	}

	@Override
	public OnlineRepairView selectDetailView(Integer id) {
		OnlineRepair onlineRepair = onlineRepairMapper.selectByPrimaryKey(id);
		OnlineRepairView view = BeanUtils.copyAs(onlineRepair, OnlineRepairView.class);
		if(view.getIsAttach() == 1) {
			Map<String, Object> map = new HashMap<>();
			map.put("type", RelationType.FAIL_REPAIR.getCode());
			map.put("objId", view.getId());
			map.put("schoolId",onlineRepair.getSchoolId());
			List<AttachmentList> attachmentList = attachmentListMapper.queryAttachmentListByRelationId(map);
			view.setAttachList(attachmentList);
		}
		return view;
	}
	
	@Override
	public int updateRepaire(FailRepairReqVO reqVO) {
		onlineRepairMapper.updateByPrimaryKeySelective(reqVO);
		new Thread(new OnlineRepairExecutor(reqVO, 2)).start();
		return 1;
	}
	
	class OnlineRepairExecutor implements Runnable {
		
		//1新增报修 2维修
		private int type;
		
		private FailRepairReqVO reqVO;
		
		public OnlineRepairExecutor(FailRepairReqVO reqVO, int type) {
			this.reqVO = reqVO;
			this.type = type;
		}

		@Override
		public void run() {
			try { 
				SchoolInfo school = schoolInfoService.queryInitWechatBySchoolId(reqVO.getSchoolId());
				String reqUrl = school.getDomainUrl() + Const.TEACHER_ROLE + 
						MenuConst.FAIL_REPAIRE + "/detail/" + reqVO.getId();
				if(type == 1) {
					String templateId = schoolWechatTemplateMapper.selectTemplateKey(
							reqVO.getSchoolId(), DictDataCode.REPAIR_APPLY.getCode());
					List<TeacherInfo> list = teacherService.selectListByRepairType(reqVO.getRepairType());
					for(TeacherInfo teacher : list) {
						school = schoolInfoService.refreshInitWechat(school);
						TemplateUtil.repairApplySend(school,teacher.getOpenid(), 
							reqVO.getRepairName(), new Date(), reqVO.getRepairPlace(), 
							reqVO.getFaultDesc(), templateId, reqUrl);
					}
				}else {
					String templateId = schoolWechatTemplateMapper.selectTemplateKey(
							reqVO.getSchoolId(), DictDataCode.REPAIRE_RESULT.getCode());
					OnlineRepair repair = onlineRepairMapper.selectByPrimaryKey(reqVO.getId());
					TeacherInfo teacher = teacherService.selectById(repair.getRepairId());
					String replyResult = StringUtils.isNotBlank(repair.getRepaireReply()) ? repair.getRepaireReply() : "已处理";
					TemplateUtil.repairResultSend(school,teacher.getOpenid(), repair.getRepairName(), 
						repair.getFaultDesc(), repair.getRepaireName(), replyResult,
						templateId, reqUrl);
				}
	        } catch (Exception e) { 
	            e.printStackTrace(); 
	        }
		}
	}

}
